package com.drill.controller.article;

import java.io.File;

import com.drill.controller.folder.Folder;
import com.drill.controller.folder.FolderService;
import com.drill.controller.interceptor.FolderInterceptor;
import com.drill.utils.UploadPathUtil;
import com.jfinal.aop.Before;
import com.jfinal.upload.UploadFile;
import com.jflyfox.jfinal.base.BaseController;
import com.jflyfox.jfinal.component.annotation.ControllerBind;


/**
* @auto ket
* @date 2016年6月30日 上午11:13:38
*/
@ControllerBind(controllerKey="/article")
public class ArticleController extends BaseController{
	
	@Before(FolderInterceptor.class)
	public void index(){
		int articleId=getParaToInt();
		
		TbArticle article= new FrontCacheService().getArticle(articleId);
		
		setAttr("item", article);
		
		Folder folder = new FolderService().getFolder(article.getFolderId());

		setAttr("folder", folder);
		
		// 活动目录
		setAttr("folders_selected", article.getFolderId());
		
		render("/pages/article/show_article.html");
		
		
	}
	
	public void add_article(){
		
		String $select=new FolderService().getSelect(1);
		
		log.info("select :"+$select);
		
		setAttr("folderSelect", $select);
		
		render("/pages/upload_file.html");
		
	}
	
	public void upload_file(){
		UploadFile uploadImage=getFile("model.file_url", "temp",10485760);
		log.info(uploadImage.toString());
		
		TbArticle article=getModel(TbArticle.class);
		
		if(uploadImage!=null){
			String fileName=UploadPathUtil.renameFile(UploadPathUtil.getUploadFilePath("image", "test"), uploadImage);
			article.set("file_url", UploadPathUtil.getUploadFilePath("image", "test")+File.separator+fileName);
		}
		
		String now = getNow();
		article.put("update_id", 1);
		article.put("update_time", now);
		article.put("create_id", 1);
		article.put("create_time", now);
		
		article.save();
		
		renderJson("{\"sc\":\"0\"}");
	}

}
